var serieTemplate = '<li class="serie buttonMenu"></li>';

/**
 * Go to serie choice
 */
function displaySeries() {
	hideContainers();
	showContainer('serieChoiceContainer');
}

/**
 * Going back from Series
 */
function backFromSeries() {
	if (hasOnlyOneExercice && hasOnlyOneBatterie) {
		displayMainMenu();
	}else{
		if(hasOnlyOneBatterie) {
			displayBatteries();
		} else {
			displayExercices();
		}
	}
}

/**
 * Empty series
 */
function emptySeries() {
	$('.menu.series').html('');
}

/**
 * Fill serie
 * @param idxSerie
 * @param serie
 */
function fillSerie(idx, serie) {
	var shortKey = $('.menu.series li').size() + 1;
	var serieDom = $(serieTemplate);
	var serieName = 'Série n°' + idx;
	if (selectedBatterie.series[idx] !== undefined) {
		serieName = selectedBatterie.series[idx].nom;			
	}
	serieDom.attr('id', 'serie_' + idx).attr('serie', idx);
	serieDom.addClass('shortKey' + shortKey);
	serieDom.append($('<div class="shortKeyIcon"></div>')).append($('<a/>').html(serieName).attr('href', '#').click(
			function(){
				selectSerie(serie, idx);
				return false;
			}
		));
	var currentRow = parseInt((shortKey - 1) / 3);
	if ($(".menu.series div.rowContainer")[currentRow] === undefined) {
		$(".menu.series").append($("<div/>").addClass("rowContainer"));
	}
	$($(".menu.series div.rowContainer")[currentRow]).append(serieDom);
}

/**
 * Select serie
 * @param idx
 */
function selectSerie(serie, idx) {
	selectedSerie = serie;
	selectedSerieIdx = idx;
	maxWordPerPage = 0;
	
	if (selectedBatterie.series[idx] !== undefined) {
		$('.serieDescription').html(selectedBatterie.series[idx].description);
	}else{
		$('.serieDescription').html("");
	}

	// if the serie has just 1 page : it's a list !
	var isList = objectLength(selectedSerie.page) == 1;
	
	// fill words
	emptyWords();
	selectedWords = new Array ();
	$.each(serie.page, fillWords);
	
	// add "numeric titles"
	var numericTitleDom = $(pageTemplate);
	for(var i=1 ; i <= maxWordPerPage ; i++) {
		numericTitleDom.append($('<a></a>').html(i).addClass('numericTitle').click(togleQuestionsOfIdx.bind(this, i)));
	}
	$('.menu.words').prepend(numericTitleDom);
	
	// add "select all" button
	numericTitleDom.prepend($('<a></a>').attr('href', '#').html('Tout').addClass('selectAll').click(selectAllWords));
	
	// display options ... or not
	var dicteeOptDom = $('#exerciceOptions .menuItem.dictee');
	dicteeOptDom.find('input').removeAttr('checked');
	if (selectedBatterie.dictee_enabled == 1) {
		dicteeOptDom.removeClass('hidden');
		if(user.default_dictee == "1") {
			dicteeOptDom.find('input').prop('checked', 'checked');
		}
		if(selectedBatterie.numero_enabled != 1) {
			dicteeOptDom.find('input').prop('checked', 'checked');
			dicteeOptDom.addClass('hidden');
		}
	}else{
		dicteeOptDom.addClass('hidden');
	}
	
	if (isList) {
		$('.menu.words').addClass('isList');
		$('.trainingContainer').addClass('isList');
	}else{
		$('.menu.words').removeClass('isList');
		$('.trainingContainer').removeClass('isList');
	}
	
	// display the right frame
	displayWords();
	
	// ellipsis
	$('.question').ThreeDots({text_span_class: 'questionText', max_rows: 1});
}
